package com.wanmait.spacesys.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.spacesys.dao.CommentReplyDAO;
import com.wanmait.spacesys.dao.PostNumberLikesDAO;
import com.wanmait.spacesys.vo.Post;
import com.wanmait.spacesys.vo.Userinfo;

/**
 * 点赞数量:控制层(帖子)
 */
@WebServlet("/PostNumberLikesController")
public class PostNumberLikesController extends BaseController {
	


	protected void postNumberLikes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
	    String pageNumStr=request.getParameter("pageNum");
	    int pageNum=1;
	    try {
			pageNum=Integer.parseInt(pageNumStr);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			/* e.printStackTrace(); */
		}
	   
	    
	    int count=PostNumberLikesDAO.getPostNumberLikesDAO().getCount(userinfo.getId());
    	int pageCount = (count+1)/2;//总页数
	    //pageNum 当前页   (最后一页)
	    if(pageNum>pageCount)
	    {
	    	pageNum=pageCount;
	    } 
	    
	    
	    if(userinfo==null) {
	    	this.redirect("/web/user/updateinformation.jsp", request, response);
	    }else {

		List<Post> posts=PostNumberLikesDAO.getPostNumberLikesDAO().findAllPostUserinfo(userinfo.getId(),pageNum);
	    request.setAttribute("posts", posts);
		request.setAttribute("count", count);
	    this.forward("/web/user/MyPost.jsp", request, response);
	    }
	}

	//假删除帖子    ajax
	protected void deletePost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String  ids=request.getParameter("id");
		Integer id=Integer.parseInt(ids);
		PostNumberLikesDAO.getPostNumberLikesDAO().deletePost(id);
	}
	
//	//根据帖子id查点赞帖子的内容
//	protected void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		String ids=request.getParameter("id");
//		Integer id=Integer.parseInt(ids);
//		Userinfo userinfo=(Userinfo)request.getSession().getAttribute("userinfo");
//		if(userinfo!=null) {
//			Post post=CommentReplyDAO.getCommentReplyDAO().myfindAllByPostId(userinfo.getId(),id);
//			request.setAttribute("post", post);
//			this.forward("/web/user/commentDisplay.jsp", request, response);
//		}
//		
//		
//				
//	}
	@Override
	protected void doDefault(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.postNumberLikes(request, response);
		
	}
	

}
